<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/7/30
 * Time: 11:17
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Shop extends Base
{
    /**
     * 店铺列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $shop_name = trim(input('shop_name'), ' '); // 店铺名
            $shop_user_name = trim(input('shop_user_name'), ' '); // 店主名称
            $shop_user_phone = trim(input('shop_user_phone'), ' '); // 店主手机
            $shop_status = input('shop_status'); // 店铺状态   1 正常   2 禁用
            
            $where['shop_status'] = ['between', [1, 2]];
            if(isset($shop_name) && $shop_name != ''){
                $where['shop_name'] = ['like', '%' . $shop_name . '%'];
            }
            if(isset($shop_user_name) && $shop_user_name != ''){
                $where['shop_user_name'] = ['like', '%' . $shop_user_name . '%'];
            }
            if(isset($shop_user_phone) && $shop_user_phone != ''){
                $where['shop_user_phone'] = ['like', '%' . $shop_user_phone . '%'];
            }
            if(isset($shop_status) && $shop_status != ''){
                $where['shop_status'] = $shop_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shop')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('shop')
                ->field('shop_id, shop_name, shop_image, shop_addr, shop_type, shop_user_name, shop_user_phone, 
                shop_number, shop_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('shop_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加店铺
     * @return mixed|\think\response\Json
     */
    public function shopAdd(){
        if(Request::instance()->isPost()){
            $shop_name = trim(input('shop_name'));
            $shop_image = input('shop_image');
            $shop_type = trim(input('shop_type'));
            $shop_user_name = trim(input('shop_user_name'));
            $shop_user_phone = trim(input('shop_user_phone'));
            $shop_sheng = input('shop_sheng');
            $shop_shi = input('shop_shi');
            $shop_qu = input('shop_qu');
            $shop_addr = input('shop_addr');
            $shop_lon = input('shop_lon');
            $shop_lat = input('shop_lat');
            $shop_intro = input('shop_intro');
            
            $insert = [
                'shop_name' => $shop_name,
                'shop_image' => $shop_image,
                'shop_addr' => $shop_addr,
                'shop_sheng' => $shop_sheng,
                'shop_shi' => $shop_shi,
                'shop_qu' => $shop_qu,
                'shop_lon' => $shop_lon,
                'shop_lat' => $shop_lat,
                'shop_type' => $shop_type,
                'shop_user_name' => $shop_user_name,
                'shop_user_phone' => $shop_user_phone,
                'shop_intro' => $shop_intro,
                'shop_time' => date('Y-m-d H:i:s')
            ];
            $res = db('shop')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 省
        $province = db('area')->where('level', 1)->field('district_id, district')->select();
        $this->assign('province', $province);
        return $this->fetch();
    }
    
    /**
     * 店铺详情
     * @return mixed
     */
    public function shopDetail(){
        $shop_id = input('shop_id');
        $shop = db('shop')
            ->join('__AREA__ a1', 'a1.district_id = shop_sheng', 'LEFT')
            ->join('__AREA__ a2', 'a2.district_id = shop_shi', 'LEFT')
            ->join('__AREA__ a3', 'a3.district_id = shop_qu', 'LEFT')
            ->field('shop_name, shop_image, shop_addr, shop_type, shop_user_name, shop_user_phone, 
            shop_number, shop_intro, shop_status, shop_time, a1.district sheng, a2.district shi, 
            a3.district qu')
            ->where('shop_id', $shop_id)
            ->find();
        return $this->fetch('', ['shop' => $shop]);
    }
    
    /**
     * 编辑店铺
     * @return mixed|\think\response\Json
     */
    public function shopEdit(){
        $shop_id = input('shop_id');
        if(Request::instance()->isPost()){
            $shop_name = trim(input('shop_name'));
            $shop_image = input('shop_image');
            $shop_type = trim(input('shop_type'));
            $shop_user_name = trim(input('shop_user_name'));
            $shop_user_phone = trim(input('shop_user_phone'));
            $shop_sheng = input('shop_sheng');
            $shop_shi = input('shop_shi');
            $shop_qu = input('shop_qu');
            $shop_addr = input('shop_addr');
            $shop_lon = input('shop_lon');
            $shop_lat = input('shop_lat');
            $shop_intro = input('shop_intro');
    
            $update = [
                'shop_id' => $shop_id,
                'shop_name' => $shop_name,
                'shop_addr' => $shop_addr,
                'shop_sheng' => $shop_sheng,
                'shop_shi' => $shop_shi,
                'shop_qu' => $shop_qu,
                'shop_lon' => $shop_lon,
                'shop_lat' => $shop_lat,
                'shop_type' => $shop_type,
                'shop_user_name' => $shop_user_name,
                'shop_user_phone' => $shop_user_phone,
                'shop_intro' => $shop_intro,
                'shop_update_time' => date('Y-m-d H:i:s')
            ];
            if($shop_image) $update['shop_image'] = $shop_image;
            $res = db('shop')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $shop = db('shop')->where('shop_id', $shop_id)
            ->field('shop_name, shop_image, shop_addr, shop_sheng, shop_shi, shop_qu, shop_lon, 
            shop_lat, shop_type, shop_user_name, shop_user_phone, shop_intro')
            ->find();
        $province = db('area')->where('level', 1)->field('district_id, district')->select();
        $city = db('area')->where('pid', $shop['shop_sheng'])->field('district_id, district')->select();
        $district = db('area')->where('pid', $shop['shop_shi'])->field('district_id, district')->select();
        return $this->fetch('', ['shop' => $shop, 'province' => $province, 'city' => $city, 'district' => $district, 'shop_id' => $shop_id]);
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function shopDown(){
        $shop_id = input('id');
        $res = db('shop')->where('shop_id', $shop_id)->setField('shop_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function shopUp(){
        $shop_id = input('id');
        $res = db('shop')->where('shop_id', $shop_id)->setField('shop_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function shopDel(){
        $shop_id = input('id');
        $res = db('shop')->where('shop_id', $shop_id)->setField('shop_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}